/**
*  \file dtc.h
*
*  \brief
*	DTC API
*  \author nan.jiang@linde-china.com
*
*  \date  2020-03-06
*
*/
#ifndef DTC_H_INCLUDED
#define DTC_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "../cfg/prj.h"

/*================================================[ public defines   ]================================================*/
/*
bit15-bit12:	occur counter  reserved
bit11 		servicedOrNot  reserved
bit10 		active	
bit9	-bit0:	DTC Code Number
*/
#define MAX_TOTAL_HISTORYDTC_TYPE0                   (  8u   ) 
#define MAX_PUSH_HISTORYDTCINDEX_TYPE0               (  MAX_TOTAL_HISTORYDTC_TYPE0-1 ) 
#define MAX_POP_HISTORYDTCINDEX_TYPE0                (  MAX_TOTAL_HISTORYDTC_TYPE0-1 ) 
#define MAX_TOTAL_HISTORYDTC_TYPE1                   (  8u   ) 
#define MAX_PUSH_HISTORYDTCINDEX_TYPE1               (  MAX_TOTAL_HISTORYDTC_TYPE1-1 ) 
#define MAX_POP_HISTORYDTCINDEX_TYPE1                (  MAX_TOTAL_HISTORYDTC_TYPE1-1 ) 
#define MAX_TOTAL_HISTORYDTC_TYPE2                   (  8u   ) 
#define MAX_PUSH_HISTORYDTCINDEX_TYPE2               (  MAX_TOTAL_HISTORYDTC_TYPE2-1 ) 
#define MAX_POP_HISTORYDTCINDEX_TYPE2                (  MAX_TOTAL_HISTORYDTC_TYPE2-1 ) 

#define	USE_NON_STANDARD_DTC_API		(0u)
#define	DTCCODE_MASK					(0X3FFu)
#define ACTIVE_MASK             		(0X0800u)
#define	DTC_TYPE2_EXTRA_SIZE			(16u)
#define ACTIVE_DTC_TOTAL_NUM			(MAX_TOTAL_HISTORYDTC_TYPE0+MAX_TOTAL_HISTORYDTC_TYPE1+MAX_TOTAL_HISTORYDTC_TYPE2)
//#define ACTIVE_DTC_TOTAL_NUM			(20)	/* throughput: max DTC count happened at the same time */
#define DTC_HIST_REQ_BUF_SIZE   		(ACTIVE_DTC_TOTAL_NUM)
#define	TYPE0_END						(99U)
#define	TYPE1_END						(199U)
#define	TYPE2_END						(999U)//(0X3FF)
#define	TYPE1_STATUS_LEN				(14)	/* Do not use sizeof(histDtcStatusType1_t)-2 */
#define	TYPE2_STATUS_LEN				(30)	/* Do not use sizeof(histDtcStatusType2_t) */
#define	STATUS_SYS_INFO_LEN				(12)	/* */

/* interface with DSC in project 1275*/
#define DTC_DONTSTORE()					kwpservices_get_passive_mode()
#define	DTC_DTCMASK						(DTCCODE_MASK)


/** maximum number of actual DTCs */
#define DTC_ACTMAX 64

/** start of DTC warning values */
#define DTC_STARTWRN       (100U)

/** start of DTC error values */
#define DTC_STARTERR       (200U)

/** start of slave DTC values */
#define DTC_SLAVEERR       (800U)

/** maximum DTC value */
#define DTC_MAXNUM         (999U)

/** service marker value */
#define DTC_SM            (1000U)

/** master service marker value */
#define DTC_MSM           (1001U)

#define DTCREC_EXTRA (8U)

#define CNT16(lcode)  ((lcode) & 0x000fU)

#define CNT32(lcode) (((lcode) & 0x00f0U) >> 4U)

#define LCODE_WORDSIZE(lcode) (CNT16(lcode) + 2 * CNT32(lcode))

#define DTC_ACTMASK           (ACTIVE_MASK)



/*================================================[ public datatypes ]================================================*/
typedef enum
{
	ACTIVEDTC_DECREASE   		= -1,  
	ACTIVEDTC_NO_CHANGE   		= 0,
	ACTIVEDTC_ADD   			= 1,   
} activeDtcTrigger_Result;

typedef enum
{
	HISTDTC_STORE_FAIL   			= -1,   
	HISTDTC_STORE_ALREADY_EXIST   	= 0, 
	HISTDTC_STORE_SUCCESS   		= 1,   
} histDtcTrigger_Result;

typedef enum
{
	TriggerReject   		= 0, 
	TriggerActive   		= 1,   
	TriggerActiveAndHist   	= 2,   
} dtcTrigger_Result;

typedef union
{
	U8  bytes[14];
	struct
	{
		U32 		TWT;
		U32 		CWT;
		U32 		DT;
		S16 		ExtraData;
	}atom;
}histDtcStatusType1_t;


typedef union
{
	struct
	{
		U32 				TWT;
		U32 				CWT;
		U32 				DT;
		U8 					Len1;
		U8					Len2;
		U16 				ExtraData[DTC_TYPE2_EXTRA_SIZE/2U];
	}atom;
}__attribute__((__packed__)) histDtcStatusType2_t;

typedef struct
{
	U16 DTC;
	U16	Length;
	U16 ExtraData[DTC_TYPE2_EXTRA_SIZE/2u];
	//U8  ExtraLen;
}request_hist_dtc_st;



/*================================================[ public variables ]================================================*/
extern U16 	activeDTCbuffer[ACTIVE_DTC_TOTAL_NUM];
extern U8 	totalActive;
extern U8 	RequestCount;
extern U8 	pushRequestIndex;
extern U8 	popRequestIndex;
extern U8 	RequestCount;
extern BO  flag_has_dtc_changes;
extern BO  fDTCMaster;
extern struct nv_readWriteparam* const pDtcOperater;
extern struct nv_dtcCodelist* 	const pDtcCodeList;
extern struct nv_dtcStatusList* const pDtcStatusList;
extern U16 historyDtcBuffer_Type0[MAX_TOTAL_HISTORYDTC_TYPE0];
extern U16 historyDtcBuffer_Type1[MAX_TOTAL_HISTORYDTC_TYPE1];
extern U16 historyDtcBuffer_Type2[MAX_TOTAL_HISTORYDTC_TYPE2];

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/
/** Set a DTC to active/inactive state */
void ErrCheck( U16 dtc,BO active);
void dtc_init(void);
BO reset_dtcStorageHandler(void);
histDtcTrigger_Result historyDtcTrigger(void);
activeDtcTrigger_Result activeDtcTrigger(U16 dtc_code,BO active);
#if 0	//2020-06-01 comment unUsed API
dtcTrigger_Result kwp_dtc_trigger(U16 dtc_code,BO active);	/* this is for DTC RANGE [0,99] */
dtcTrigger_Result kwp_dtc_trigger_with_one_s16_extra(U16 dtc_code, BO active, S16 ext1);	/* this is for DTC RANGE [100,199] */
dtcTrigger_Result kwp_dtc_trigger_with_four_f32_extra(U16 dtc_code, BO active, F32 ext1,F32 ext2,F32 ext3, F32 ext4);	/* this is for DTC RANGE [200,999]  with_four_f32_extra */
dtcTrigger_Result kwp_dtc_trigger_with_fourS16_twoF32_extra(U16 dtc_code, BO active,S16 ext1_s16,S16 ext2_s16,F32 ext3_s16,F32 ext4_s16, F32 ext5_f32,F32 ext6_f32); /* this is for DTC RANGE [200,999] with_fourS16_twoF32_extra */
#endif
extern void getDtcListOver_200(U16 dtcList[]);
/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
*                             Copyright 2017 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
